using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography.X509Certificates;

namespace TBOCrypto
{
    class CertsMan
    {
        static X509Store store;

        public static string GetStoreInfo()
        {
            string output = "";
            store.Open(OpenFlags.ReadOnly);

            foreach (X509Certificate2 cert in store.Certificates)
            {
                //output += "Certificate info:\r\n";
                //output += "Subject: " + cert.Subject + "\r\n";
                //output += "NotBefore: " + cert.NotBefore + "\r\n";
                //output += "NotAfter: " + cert.NotAfter + "\r\n";
                //output += "Name info: " + cert.GetNameInfo(X509NameType.DnsName, true) + "\r\n";
                //output += "------------------------------------------------------------------\r\n\r\n";
                //output += "Issuer: " + cert.Issuer + "\r\n";
                //output += "Version: " + cert.Version + "\r\n";
                //output += "Thumbprint: " + cert.Thumbprint + "\r\n";
                //output += "Public Key Format: " + cert.PublicKey.Oid.FriendlyName + "\r\n";
                output += cert.ToString(true) + "\r\n";
                output += "------------------------------------------------------------------\r\n\r\n";
                //output += "XML: " + cert.PublicKey.Key.ToXmlString(false) + "\r\n";
            }

            store.Close();

            if (output.Equals(""))
            {
                output += "No certificates";
            }

            return output;
        }

        private static void LoadPersonalStore()
        {
            store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
        }

        private static void LoadRootStore()
        {
            store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
        }

        public static string GetPersonalStoreInfo()
        {
            LoadPersonalStore();
            return GetStoreInfo();
        }

        public static string GetRootStoreInfo()
        {
            LoadRootStore();
            return GetStoreInfo();
        }

        public static void AddCertToStore(string certPath, string pass)
        {
            X509Certificate2 cert = new X509Certificate2(certPath, pass);
            store.Open(OpenFlags.ReadWrite);
            store.Add(cert);
            store.Close();
        }

        public static void ClearStore()
        {
            store.Open(OpenFlags.ReadWrite);
            store.RemoveRange(store.Certificates);
            store.Close();
        }

        public static X509Certificate2 GetMyCertificate()
        {
            LoadPersonalStore();
            store.Open(OpenFlags.ReadOnly);
            X509Certificate2 cert = store.Certificates[0];
            store.Close();
            return cert;
        }

        public static X509Certificate2 GetRecipientCertificate()
        {
            //LoadRootStore();
            LoadPersonalStore();
            store.Open(OpenFlags.ReadOnly);
            X509Certificate2 cert = store.Certificates[1];
            store.Close();
            return cert;
        }
    }

    
}
